package com.example.demo.widget.chart.utils

/**
 * 图形绘制文字工具类
 */
object ChartTextUtils {
    fun ellipseTextEnd(text: String?,count: Int): String{
        if(text == null || text.isEmpty()){
            return ""
        }
        if(text.length <= count){
            return text
        }

        val sub = text.substring(0,count)
        return "$sub..."
    }

    fun commaSeparationNumber(value: Int): String{
        val text = value.toString()
        if(value < 1000){
            return text
        }
        val array = text.toCharArray().reversedArray()
        val sb = StringBuilder()
        val size = array.size
        for(index in 0 until size){
            sb.insert(0,array[index])
            if(index % 3 == 2 && index != size - 1){
                sb.insert(0,",")
            }
        }
        return sb.toString()
    }

    fun commaSeparationNumber(value: Double): String{
        val text = value.toString()
        if(value < 1000){
            return text
        }
        val indexDot = text.lastIndexOf(".")
        var intPartText = ""
        var otherText = ""
        if(indexDot < 0){
            intPartText = text
        }else{
            intPartText = text.substring(0,indexDot)
            otherText = text.substring(indexDot)

        }

        val array = intPartText.toCharArray().reversedArray()
        val sb = StringBuilder()
        val size = array.size
        for (index in 0 until size) {
            sb.insert(0, array[index])
            if (index % 3 == 2 && index != size - 1) {
                sb.insert(0, ",")
            }
        }
        sb.append(otherText)
        return sb.toString()
    }
}